package com.bitters.contorller;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author thomas.he
 * @date 2022/6/27
 **/
@RestController
public class IndexContorller {



    /**
     * 每个放入缓存中的，都需要指定放入到那个缓存里面去
     */
    @GetMapping("/index")
    @Cacheable(value = {"categroy"}, key = "#root.method.name")
    //@CachePut("categroy")//每次都会执行方法，并将结果存入指定的缓存中
    public Map<String, String> index() {
         Map<String, String> localMap = new ConcurrentHashMap();
        System.out.println("index请求被调用了....");
        localMap.clear();
        localMap.put("1", "hello2");
        localMap.put("2", "hello2");
        return localMap;
    }

    /**
     * 持久化时可以先清空数据
     *
     * @return
     */
    @CacheEvict(value = "categroy", allEntries = true)
    @GetMapping("/update")
    public Map<String, String> updateCascade() {
         Map<String, String> localMap = new ConcurrentHashMap();
        System.out.println("update请求被调用了....");
        localMap.put("1", "hello2");
        localMap.put("2", "hello2");
        localMap.put("3", "hello2");
        localMap.put("4", "hello2");
        return localMap;
    }

}
